﻿using Autofac;
using Autofac.Integration.Mvc;
using DAL;
using Data.MyDbContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;

namespace WebIndex.App_Start
{
    public class AutofacConfig
    {
        public IContainer myContainer { get; private set; }

        public void Configure()
        {
            ContainerBuilder builder = new ContainerBuilder();
            OnConfigure(builder);

            if (this.myContainer == null)
            {
                this.myContainer = builder.Build();
            }
            else
            {
                builder.Update(this.myContainer);
            }

            //This tells the MVC application to use myContainer as its dependency resolver
            DependencyResolver.SetResolver(new AutofacDependencyResolver(this.myContainer));
        }


        protected virtual void OnConfigure(ContainerBuilder builder)
        {
            //register HttpContext
            builder.RegisterModule(new AutofacWebTypesModule());

            //helper classes
            builder.RegisterType<DbContextFactory>().InstancePerHttpRequest();
            builder.RegisterType<UnitOfRepositories>().InstancePerHttpRequest();

            //builder.RegisterType<ManageInformation>().As<IManageInformation>().InstancePerHttpRequest();
            //builder.RegisterType<AccessPredicateFactory>().As<IAccessPredicateFactory>().InstancePerHttpRequest();
            //builder.RegisterType<GetPermission>().As<IGetPermissions>().InstancePerHttpRequest();

            //register DBContext and Entities
            //builder.RegisterType<DbContextFactory>().InstancePerHttpRequest();
            //builder.RegisterType<UserRepository>().As<IUserRepository>().InstancePerHttpRequest();

            //register controllers
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
        }
    }
}